package application;


import controller.ATM;
import controller.ATMController;
import domain.Receipt;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.io.InputStream;

public class AtmFrame extends Application {
    private Stage stage;

    private ATMController atmController;

    private ATM atm;

    public AtmFrame() {
        System.out.println("atmF被实例化");
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            stage = primaryStage;
            // Initialize the Stage with type of modal
            // stage.initModality(Modality.APPLICATION_MODAL);
            // Set the owner of the Stage
            // stage.initOwner(primaryStage)
//			Parent root = FXMLLoader.load(getClass().getResource("/view/pluginCard.fxml"));
//			Scene scene = new Scene(root);
//			// stage.getIcons().add(new Image("/images/ͼ��1.jpg"));
//			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
//			stage.setScene(scene);
            stage.setTitle("取款");
            stage.setMinWidth(665);
            stage.setMinHeight(460);
            turnOn();
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //ATM开机
    public void turnOn() {
        try {
            atmController = (ATMController) replaceSceneContent("/view/pluginCard.fxml");
            atmController.setApp(this);
//            Parent root = FXMLLoader.load(getClass().getResource("/view/pluginCard.fxml"));
//            Scene scene = new Scene(root);
//            stage.setScene(scene);
            atm = new ATM();
            atm.turnOn();
        } catch (Exception ex) {
            ex.getMessage();
        }

    }

    //ATM插卡
    public void pluginCard(String cardId) {
        try {
            if (atm.pluginCard(cardId)) {
                atmController = (ATMController) replaceSceneContent("/view/inputPassword.fxml");
                atmController.setApp(this);

            } else {

            }

        } catch (Exception e) {

        }


    }

    //退卡
    public void exitCard() throws IOException {
        // 返回插卡页面
        try {
            atmController = (ATMController) replaceSceneContent("/view/pluginCard.fxml");
            atmController.setApp(this);

        } catch (Exception e) {

        }
    }

    //登录
    public void LoginAccount(String password) {
        if (atm.LoginAccount(password)) {
            try {
                atmController = (ATMController) replaceSceneContent("/view/index.fxml");
                atmController.setApp(this);
            } catch (Exception e) {

            }

        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.titleProperty().set("错误");
            alert.headerTextProperty().set("密码错误");
            alert.showAndWait();
        }
    }

    //取款
    public void withdrawl() {
        try {
            atmController = (ATMController) replaceSceneContent("/view/withdraw.fxml");
            atmController.setApp(this);

        } catch (Exception e) {

        }

    }

    //取款交易
    public void confirmWithdrawl(String amount) {
        try {
            //如果金额为空或为0
            if(amount.equals("")|| amount == null || Double.valueOf(amount) == 0){
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.titleProperty().set("警告");
                alert.headerTextProperty().set("金额不能为空或者0");
                alert.showAndWait();

            }else {
            if (atm.confirmWithdrawl(amount)) {
                atmController.setApp(this);
                atmController.setCash(amount);
                //弹出交易成功提示框
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.titleProperty().set("成功");
                alert.headerTextProperty().set("交易成功");
                alert.showAndWait();
            } else {
                //余额不足提示框
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.titleProperty().set("失败");
                alert.headerTextProperty().set("交易失败，余额不足");
                alert.showAndWait();
            }
            }

        } catch (Exception e) {

        }

    }

    //回到选择界面
    public void back() {
        try {
            atmController = (ATMController) replaceSceneContent("/view/index.fxml");
            atmController.setApp(this);

        } catch (Exception e) {

        }
    }
    //确认打印凭证
    public  void  receiptPrint(){
        print(atm.receiptPrint());
    }
    //打印
    public void print(Receipt receipt){
        atmController.setApp(this);
        atmController.print(receipt);
    }

    private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = AtmFrame.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(AtmFrame.class.getResource(fxml));
//        Parent root = loader.load(getClass().getResource(fxml));

        AnchorPane page = null;
        try {

            page = (AnchorPane) loader.load(in);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 748, 600);

        //添加样式表
//		scene.getStylesheets().add(getClass().getResource("/view/application.css").toExternalForm());
//        scene.getStylesheets().add(getClass().getResource("/view/MainStyle.css").toExternalForm());


        stage.setScene(scene);
        stage.sizeToScene();

        return (Initializable) loader.getController();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
